const db = wx.cloud.database();
const goodsDetail = db.collection("goodsDetail");
const goods = db.collection("goods");
const comments = db.collection("comments");
const shopCar = db.collection("shopCar");
const userLove = db.collection("userLove");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsDetail: [],
    bannerImg: [],
    good: {},
    goodTextIntro: [],
    comment: {},
    tapDatabaseName: null,
    chooseColumn: [],
    goodsId: '',
    star: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const goodname = options.goodname;
    const goodsId = options.goodsId;
    const tapDatabaseName = options.tapDBName;
    console.log("tapDatabaseName" + tapDatabaseName + 'goodsId' + goodsId + " " + "goodname" + goodname);
    const goodTextIntroDB = db.collection(tapDatabaseName);

    this.setData({
      goodsId: goodsId,
      tapDatabaseName: tapDatabaseName,
      goodname: goodname,
      goodTextIntroDB: goodTextIntroDB
    })
    goodsDetail
      .where({
        name: goodname,
        title: 'detailImg'
      }).
    get().then(res => {
        this.setData({
          goodsDetail: res.data
        })
      }),
      goodsDetail
      .where({
        name: goodname,
        title: 'headerImg'
      }).
    get().then(res => {
        console.log(res.data)
        this.setData({
          bannerImg: res.data
        })
      }),
    //获取该商品的信息存放在数组goodTextIntro中
      goodTextIntroDB.doc(goodsId)
      .get().then(res => {
        console.log(res.data)
        this.setData({
          goodTextIntro: res.data
        })
        this.starStatus();
      })
    //评论
    comments.where({
        name: goodname
      })
      .limit(1).get().then(res => {
        //console.log(res.data[0].comments[0].text)
        if (res.data[0]) {
          this.setData({
            comment: res.data[0].comments[0]
          })
        }
      })
 
      
  },

  onTapComment: function (event) {
    wx.navigateTo({
      url: 'comment/comment',
    })
  },
  //收藏状态
  starStatus:function(){
    wx.cloud.callFunction({
      name: 'searchMyLove',
      data: {
        id: this.data.goodTextIntro._id
      }
    }).then(res => {
      console.log(res.result.data.length)
      console.log(res.result.data)
      if (res.result.data.length == 0) {
        this.setData({
          star: false
        })
      }else{
        this.setData({
          star: true
        })
      }
    })
  },

  //收藏
  onClickStar: function (event) {
    //点击时判断是否数据库中是否存在该商品，如果我的收藏里面没有该商品，则加入，如果有则删除
    wx.cloud.callFunction({
      name: 'searchMyLove',
      data: {
        id: this.data.goodTextIntro._id
      }
    }).then(res => {
      console.log(res.result.data.length)
      console.log(res.result.data)
      if (res.result.data.length == 0) {
        this.setData({
          star: true
        })
        wx.cloud.callFunction({
          name: "goodsAddToShopCar",
          data: {
            dbName: 'userLove',
            id: this.data.goodTextIntro._id,
            price: this.data.goodTextIntro.price,
            text: this.data.goodTextIntro.text,
            src: this.data.goodTextIntro.src,
            name: this.data.goodTextIntro.name,
            title: this.data.goodTextIntro.Info,
            num: 1,
            choosed: false,
            goodsDBName: this.data.tapDatabaseName
          }
        }).then(res => {
          console.log(res.result)
        })
      } else {
        this.setData({
          star: false
        })
        wx.cloud.callFunction({
          name: 'deleteMyLove',
          data: {
            id: this.data.goodTextIntro._id,
          }
        }).then(res => {
          console.log(res.result)
        })
      }
    })
  },
  //跳转到购物车
  onClickCart: function (event) {

    wx.switchTab({
      url: '../shopCar/shopCar',
    })
  },
  //跳转到店铺
  onClickStore: function (event) {
    wx.navigateTo({
      url: '../store/store'
    })
  },
  //加入购物车
  onClickAddToCart: function (event) {
    wx.showToast({
      title: '加购成功',
      duration: 1000,
      icon: "success",
      mask: true
    })
    wx.cloud.callFunction({
      name: "goodsAddToShopCar",
      data: {
        dbName: 'shopCar',
        id: this.data.goodTextIntro._id,
        price: this.data.goodTextIntro.price,
        text: this.data.goodTextIntro.text,
        src: this.data.goodTextIntro.src,
        name: this.data.goodTextIntro.name,
        title: this.data.goodTextIntro.Info,
        num: 1,
        choosed: false,
        goodsDBName: this.data.tapDatabaseName
      }
    }).then(res => {
      console.log(res)
    })

  },
  //立即购买 
  onClickBuy: function (event) {
    wx.cloud.callFunction({
      name: 'updateAllChoosed',
      data: {
        ostates: true,
        nstates: false
      }
    }).then(res => {
      console.log(res.result)
      wx.cloud.callFunction({
        name: "goodsAddToShopCar",
        data: {
          dbName: 'shopCar',
          id: this.data.goodTextIntro._id,
          price: this.data.goodTextIntro.price,
          text: this.data.goodTextIntro.text,
          src: this.data.goodTextIntro.src,
          name: this.data.goodTextIntro.name,
          title: this.data.goodTextIntro.Info,
          num: 1,
          choosed: true,
          goodsDBName: this.data.tapDatabaseName
        }
      }).then(res=>{
        console.log(res.result)
        wx.navigateTo({
          url: '../shopCar/orderDetail/orderDetail?price=' + this.data.goodTextIntro.price * 100,
        })
      })
    }).catch(console.error);
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (event) {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})